import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';

class GTCheckBoxV3 extends StatefulWidget {
  /// 图标选中状态
  final bool isSelected;
  final double iconSize;
  final EdgeInsetsGeometry? iconPadding;
  final Function(bool) onCheckChange;

  /// 文本
  final String? title;
  final double fontSize;
  final FontWeight fontWeight;
  final Color? fontColor;
  final Color? checkboxColor;
  final bool? isCircle;
  final bool? isEnable;
  final CrossAxisAlignment? crossAxisAlignment;

  //根据文本行数控制icon的crossAxisAlignment
  final bool? needCalculate;

  const GTCheckBoxV3(
      {Key? key,
      required this.isSelected,
      required this.onCheckChange,
      this.title,
      this.iconSize = 16,
      this.iconPadding,
      this.fontSize = 14,
      this.fontWeight = FontWeight.w500,
      this.fontColor,
      this.checkboxColor,
      this.isCircle = false,
      this.isEnable = true,
      this.needCalculate = false,
      this.crossAxisAlignment = CrossAxisAlignment.center})
      : super(key: key);

  @override
  State<GTCheckBoxV3> createState() => _GTCheckBoxTextState();
}

class _GTCheckBoxTextState extends State<GTCheckBoxV3> {
  TextStyle _textStyle = gtTextStyleW500(fontSize: 14, color: GTColorsV3.text1.color);
  late CrossAxisAlignment _crossAxisAlignment;

  @override
  void initState() {
    _crossAxisAlignment = widget.crossAxisAlignment ?? CrossAxisAlignment.center;
    if (widget.iconSize == 16) {
      _textStyle = gtTextStyleW500(fontSize: 14, color: GTColorsV3.text1.color);
    } else if (widget.iconSize == 12) {
      _textStyle = gtTextStyleW400(fontSize: 12, color: GTColorsV3.text1.color);
    } else {
      _textStyle =
          gtTextStyle(fontSize: widget.fontSize, color: widget.fontColor).copyWith(fontWeight: widget.fontWeight);
    }
    super.initState();
  }

  int _calculateTextLines(BuildContext context, String text, TextStyle style, double maxWidth) {
    final TextPainter textPainter = TextPainter(
      text: TextSpan(text: text, style: style),
      maxLines: null,
      textDirection: TextDirection.ltr,
    )..layout(maxWidth: maxWidth);

    return textPainter.computeLineMetrics().length;
  }

  @override
  Widget build(BuildContext context) {
    if (widget.needCalculate ?? false) {
      return LayoutBuilder(builder: (context, constraints) {
        final maxTextWidth = constraints.maxWidth - widget.iconSize - 6;
        int textLines = _calculateTextLines(context, widget.title ?? "", _textStyle, maxTextWidth);
        _crossAxisAlignment = textLines > 1 ? CrossAxisAlignment.start : CrossAxisAlignment.center;
        return _container();
      });
    } else {
      return _container();
    }
  }

  Widget _container() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: _crossAxisAlignment,
      children: [
        GestureDetector(
          child: Container(
            padding: widget.iconPadding ?? GTEdgeInsetsOnly(right: 6),
            child: Padding(
              padding: EdgeInsets.only(top: _crossAxisAlignment == CrossAxisAlignment.center ? 0 : 2),
              child: Icon(
                widget.isCircle ?? false
                    ? widget.isSelected
                        ? GTIconFont.e7cb //圆形选中icon
                        : widget.isEnable ?? true
                            ? GTIconFont.e7de //圆形未选中可用icon
                            : GTIconFont.e7ac //圆形未选中不可用icon
                    : widget.isSelected
                        ? GTIconFont.e6ad //方形选中icon
                        : widget.isEnable ?? true
                            ? GTIconFont.e7dd //方形未选中可用icon
                            : GTIconFont.e7ad, //方形未选中不可用icon
                size: widget.iconSize,
                color:
                    widget.isEnable ?? true ? widget.checkboxColor ?? GTColorsV3.icon1.color : GTColorsV3.icon4.color,
              ),
            ),
          ),
          onTap: () {
            widget.onCheckChange(!widget.isSelected);
          },
        ),
        widget.title == null
            ? Container()
            : Expanded(
                child: GestureDetector(
                  child: GTText(
                    widget.title ?? '',
                    removeTopBottomPadding: true,
                    style: _textStyle,
                  ),
                  onTap: () {
                    widget.onCheckChange(!widget.isSelected);
                  },
                ),
              )
      ],
    );
  }
}
